package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.impl.UserDaoImpl;
import entity.User;
public class Register_Servlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("psw_one");
		String gender=request.getParameter("gender");
		int age=Integer.parseInt(request.getParameter("age"));
		String hobby=request.getParameter("hobby");
		String text=request.getParameter("text");
		int g;
		if(gender=="male") g=1;
		else g=2;
		System.out.print(username+password+g+hobby+text);
		User user=new User();
		user.setU_name(username);
		user.setU_password(password);
		user.setU_sex(g);
		user.setU_age(age);
		user.setU_hobby(hobby);
		user.setU_describe(text);
		UserDaoImpl UD= new UserDaoImpl();
		String forwardPath="/register.jsp";
		String error2="用户名已存在！";
		if(UD.findUser(username, password)){
			request.getSession().setAttribute("error2", error2);
		}else{
			UserDaoImpl UD2= new UserDaoImpl();
			UD2.insertUser(user);
			forwardPath = "/MianPagelistServlet";
		}
		response.sendRedirect(request.getContextPath()+forwardPath);
	}

}
